package com.zyq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.exception.CustomException;
import com.zyq.model.common.BaseDto;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.Article;
import com.zyq.model.entity.BloodSugar;
import com.zyq.model.request.ArticleDto;
import com.zyq.service.IBloodSugarService;
import com.zyq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zyq
 * @create: 2022-04-08
 **/

@Api( tags = "患者血糖")
@RestController
@RequestMapping("/bloodSugar")
public class BloodSugarController {


    @Autowired
    private IBloodSugarService bloodSugarService;

    @Autowired
    private UserUtil userUtil;

    @ApiOperation("cx7")
    @GetMapping("/select7dayList")
    @RequiresAuthentication
    public ResponseBean select7dayList(@RequestHeader String Authorization,@RequestParam Long userId){
        Map<String,Object> map = new HashMap<>();
        List<BloodSugar> bloodSugars = bloodSugarService.select7dayList(userId);
        map.put("list",bloodSugars);
        System.out.println(bloodSugars.get(0).getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println(formatter.format(new Date()));
        if(bloodSugars.size()>0  && formatter.format(bloodSugars.get(0).getTime()).compareTo(formatter.format(new Date()))==0){
            map.put("flag",true);
        }
        return new ResponseBean(HttpStatus.OK.value(),
                "查询成功(Query was successful)",map
                );
    }

    @ApiOperation("查询")
    @GetMapping("/list")
    public ResponseBean list(BaseDto base)
    { StringBuilder orderByStr = new StringBuilder();
        if(base.getSidx()!=null && base.getSord()!=null){
            orderByStr.append(base.getSidx());
            orderByStr.append(" ");
            orderByStr.append(base.getSord());
        }
        PageHelper.startPage(base.getPageNum(), base.getPageSize(),orderByStr.toString());
        BloodSugar bloodSugar = new BloodSugar();
        List<BloodSugar> list = bloodSugarService.selectBloodSugarList(bloodSugar);
        PageInfo<BloodSugar> selectPage = new PageInfo<BloodSugar>(list);
        if (list == null || list.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("total", selectPage.getTotal());
        result.put("pages", selectPage.getPages());
        result.put("bloodSugar", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);

    }


    @ApiOperation("新增")
    @PostMapping
    @RequiresAuthentication
    public ResponseBean add(@RequestBody BloodSugar bloodSugar)
    {
        bloodSugar.setTime(new Date())
                .setUserId(userUtil.getUserId());
        if(bloodSugarService.insertBloodSugar(bloodSugar)>0)
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseBean edit(@RequestBody BloodSugar bloodSugar)
    {
        bloodSugarService.updateBloodSugar(bloodSugar);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
    }


    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public ResponseBean remove(@PathVariable Long[] ids)
    {
        bloodSugarService.deleteBloodSugarByIds(ids);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
    }

}
